package frames;

import inputFrames.InputPanel;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import person.PersonRecorder;

import avatarFrame.AvatarFrame;
import constants.Constants;
import exerciseFrames.ExercisePanel;

public class MainFrame extends JFrame {
	private MainPanel mainPanel;
	private InputPanel inputPanel;
	private AvatarFrame avartaFrame;
	private OpenPanel openPanel;
	private ExercisePanel exercisePanel;

	// constructor
	public MainFrame() {
		setLookAndFeel();
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(Constants.FRAME_SIZE_WIDTH, Constants.FRAME_SIZE_HEIGHT);
		setFrameToCenter();

		// toolBar

		// main Panel
		mainPanel = new MainPanel();
		mainPanel.setLayout(null);
		this.add("Center", mainPanel);
		mainPanel.setSize(Constants.PANEL_SIZE_WIDTH, Constants.PANEL_SIZE_HEIGHT);
		mainPanel.setVisible(true);

		// input Panel
		inputPanel = new InputPanel();
		this.add("Center", inputPanel);
		inputPanel.setVisible(false);

		// open Panel
		openPanel = new OpenPanel();
		this.add(openPanel);
		openPanel.setVisible(false);

		// exercise Panel
		exercisePanel = new ExercisePanel();
		this.add(exercisePanel);
		exercisePanel.setVisible(false);

		avartaFrame = new AvatarFrame();

	}

	public AvatarFrame getAvartaFrame() {
		return avartaFrame;
	}

	public void setAvartaFrame(AvatarFrame avartaFrame) {
		this.avartaFrame = avartaFrame;
	}

	public void init() {
		mainPanel.init(this);
		inputPanel.init(this);
		openPanel.init(this);
		exercisePanel.init(this);
	}

	public void openAvarta(Image avartaImg, PersonRecorder personRecorder) {
		avartaFrame.drawAvarta(avartaImg);
		avartaFrame.setPersonRecorder(personRecorder);

	}

	public void setLookAndFeel() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception exc) {
			System.err.println("Theme error");
		}
	}
	
	public void setFrameToCenter() {
		Dimension monitorSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();
		if (frameSize.height > monitorSize.height) {
			frameSize.height = monitorSize.height;
		}
		if (frameSize.width > monitorSize.width) {
			frameSize.width = monitorSize.width;
		}
		this.setLocation((monitorSize.width - frameSize.width) / 2, (monitorSize.height - frameSize.height) / 2);
	}
	
	public void sendPersonRecorderToExercisePanel(PersonRecorder personRecorder, int indexOfSavedPersonRecorder) {
		exercisePanel.setPersonRecorder(personRecorder, indexOfSavedPersonRecorder);
	}

	public void setInputPanel() {
		mainPanel.setVisible(false);
		inputPanel.setVisible(true);
		openPanel.setVisible(false);
		exercisePanel.setVisible(false);
	}

	public void setMainPanel() {
		mainPanel.setVisible(true);
		inputPanel.setVisible(false);
		openPanel.setVisible(false);
		exercisePanel.setVisible(false);
	}

	public void setOpenPanel() {
		openPanel.loadData();
		mainPanel.setVisible(false);
		inputPanel.setVisible(false);
		openPanel.setVisible(true);
		exercisePanel.setVisible(false);
	}

	public void setExercisePanel() {
		mainPanel.setVisible(false);
		inputPanel.setVisible(false);
		openPanel.setVisible(false);
		exercisePanel.setVisible(true);
	}

	public void closeAvatarFrame() {
		avartaFrame.setVisible(false);
	}
}
